#include <cstdio>

const int Limit_Size = 1000 + 10;

int n, m, s[ Limit_Size ];
bool visit[ Limit_Size ];

int find( int root )
{
    if ( s[ root ] == -1 )
        return root;
    return s[ root ] = find( s[ root ] );
}

void unionSet( int root1, int root2 )
{
    root1 = find( root1 );
    root2 = find( root2 );
    if ( root1 != root2 )
        s[ root1 ] = root2;
}

void init( )
{
    int i, v1, v2;
    scanf("%d%d", &n, &m);
    for ( i = 1; i <= n; i++ )
    {
        s[ i ] = -1;
        visit[ i ] = false;
    }
    for ( i = 0; i < m; i++ )
    {
        scanf("%d%d", &v1, &v2);
        unionSet( v1, v2 );
    }
}

void print( )
{
    int i, count = 0, v;
    for ( i = 1; i <= n; i++ )
    {
        v = find( i );
        if ( !visit[ v ] )
        {
            count++;
            visit[ v ] = true;
        }
    }
    printf("%d\n", count);
}

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        init( );
        print( );
    }
    return 0;
}
